﻿package com.wxtl.ws;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.wxtl.entity.UpgradePack;
import com.wxtl.exception.ServiceException;
import com.wxtl.service.UpgradePackService;
import com.wxtl.util.UPSProtocol;
import com.wxtl.ws.qo.DownPackQo;

/**
 * 下载补丁包
 * @author hc24
 *
 */
public class DownloadAction extends UPSProtocol {
	private UpgradePackService upgradePackService;
	private String filename;
	private long contentLength;
	private String filepath;
	private DownPackQo downQo;

	public void setUpgradePackService(UpgradePackService upgradePackService) {
		this.upgradePackService = upgradePackService;
	}

	public String getFilename() {
		try {
			return new String(this.filename.getBytes(),"ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			return this.filename;
		} 
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public DownPackQo getDownQo() {
		return downQo;
	}

	public void setDownQo(DownPackQo downQo) {
		this.downQo = downQo;
	}
	

	public String getFilepath() {
		return filepath;
	}

	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}
	
	public long getContentLength() {
		return contentLength;
	}

	public void setContentLength(long contentLength) {
		this.contentLength = contentLength;
	}

	public String execute() throws Exception {
//		this.downQo=new DownPackQo();
		System.out.println("DownloadAction.java:download executing....");
//		System.out.println(downQo);
//		if (this == null)
//			throw new ServiceException("参数为空");
//		if (this.getAuthsn() == null || "".equals(this.getAuthsn().trim()))
//			throw new ServiceException("授权编号不能为空");
//		if (this.getCustsn() == null || "".equals(this.getCustsn().trim()))
//			throw new ServiceException("客户编号不能为空");
		if (this.getUpgradeId() == null||"".equals(this.getUpgradeId()))
			throw new ServiceException("upgradeid不能为空");

		UpgradePack pack = upgradePackService.downPack(this);
		File file = new File(pack.getDownpath());
		if (!file.exists())
			throw new ServiceException("下载出错,所请求的升级包不存在,请联系厂商");
		//this.filename = pack.getDownpath().substring(pack.getDownpath().indexOf("\\")+1);
		this.filename = this.getUpgradeId() + ".zip";
		this.filepath=pack.getDownpath();
		this.contentLength=file.length();
		return "success";
	}

	public InputStream getDownFile() throws Exception {
		System.out.println("DownloadAction.java:开始下载文件");
		if(this.filepath!=null&&!"".equals(this.filepath)){
			ActionContext actionContext = ActionContext.getContext();
			HttpServletResponse response = (HttpServletResponse) actionContext
			.get(ServletActionContext.HTTP_RESPONSE);
			response.addHeader(UPSProtocol.FILE_LENGTH, this.contentLength+"");
			System.out.println("DownloadAction.java:contentLength:"+contentLength);
			return new FileInputStream(this.filepath);
		}
		return null;
	}

}